import { ObservedMap } from "../libs/ObservedMap";
import { debug } from "@tauri-apps/plugin-log";

export function Ref(target: any, propertyKey: string | symbol) {
    let orig = target[propertyKey];

    Reflect.defineProperty(target, propertyKey, {
        get() {
            return orig;
        },
        set(value) {
            orig = value;
            const root = ObservedMap.viewTree.top();
            if (root !== undefined && Reflect.getMetadata('__ob', root, 'build') == true) {
                ObservedMap.componentTree.stack.forEach(e => e.remove());
                root.build.apply(this);
            }
        }
    });
    debug('Ref end')
}